package edu.clemson.wcbeck.cft;

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class CFTActivity extends Activity implements OnClickListener{
	
	private MyDatabaseHelper dataSource;
	boolean hasCamera = false;
	
	ArrayAdapter<String> adapterConf;
	ArrayAdapter<String> adapterTeam;
	
	Spinner spnnrConf;
	Spinner spnnrTeam;
	
	Button bttnGo;
	
	CharSequence toastText;
	Toast myToast;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
				
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		dataSource = new MyDatabaseHelper(this);
		try {
			dataSource.createDataBase();
		} catch (IOException e) {
			toastText = "Error creating the database!";
			myToast = Toast.makeText(this.getApplicationContext(), toastText, Toast.LENGTH_SHORT);
			myToast.show();
			e.printStackTrace();
		}
        
        try {
     		dataSource.openDataBase();
     	}catch(SQLException sqle){
     		toastText = "Error opening the database!";
			myToast = Toast.makeText(this.getApplicationContext(), toastText, Toast.LENGTH_SHORT);
			myToast.show();
     		throw sqle;
     	}
		
		bttnGo = (Button) findViewById(R.id.bttnGo);
		bttnGo.setOnClickListener(this);
		
		spnnrConf = (Spinner) findViewById(R.id.spnnrConference);
		spnnrTeam = (Spinner) findViewById(R.id.spnnrTeam);

		String[] confs = dataSource.getAllConfs();
		adapterConf = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, confs);
		adapterConf.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		String[] teams = dataSource.getTeamsByConf(1);
		adapterTeam = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, teams);
		adapterTeam.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spnnrConf.setAdapter(adapterConf);
		spnnrTeam.setAdapter(adapterTeam);    


		spnnrConf.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				fillTeamSpinner(pos);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// Do nothing.
			}
		} );
		
		dataSource.close();
	}
    
    public void initAbout(){
    	//dataSource.close();
    	
    	Intent myIntent = new Intent(getBaseContext(), CFTAbout.class);
        startActivityForResult(myIntent, 0);
    }
    
    public void initFavorites(){
    	//dataSource.close();
    	
    	Intent myIntent = new Intent(getBaseContext(), CFTFavorites.class);
    	startActivityForResult(myIntent, 0);
    }
    
    public void initMyPhotos(){
    	Intent myIntent = new Intent(getBaseContext(), PhotoIntentActivity.class);
    	startActivityForResult(myIntent, 0);
    }
    
    public void initMain(){}
    
    /*public void initExit(){
    	Intent intent = new Intent(this, FinActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	startActivity(intent);
    	finish();
    }*/
    
    public void initTeam(){
    	dataSource.openDataBase();
    	String[] teamName = { (String) spnnrTeam.getSelectedItem() };
    	int id = dataSource.getTeamIdByName(teamName);
    	Team team = dataSource.createTeam(id);
    	dataSource.close();
    	
    	Bundle mySelection = new Bundle();
    	mySelection.putParcelable("Team", team);
    	mySelection.putInt("team_id", spnnrTeam.getSelectedItemPosition());
    	
    	Intent myIntent = new Intent(getBaseContext(), CFTTeamTabs.class);
    	myIntent.putExtras(mySelection);
        startActivityForResult(myIntent, 0);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
	        case R.id.icAbout: initAbout();
	        	break;
	        case R.id.icFavorites: initFavorites();
	        	break;
	        case R.id.icCaptureMedia: initMyPhotos();
	        	break;
        }
        return true;
    }
	
    
    public void fillTeamSpinner(int pos){
    	dataSource.openDataBase();
    	String[] teams = dataSource.getTeamsByConf(pos+1);
		adapterTeam = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, teams);
    	adapterTeam.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnnrTeam.setAdapter(adapterTeam);
		dataSource.close();
    }


    @Override
    public void onClick(View v) {
    	if (v == bttnGo){

    		//long id = 0;

    		CharSequence text;
    		Context context = getApplicationContext();
    		int pos = spnnrTeam.getSelectedItemPosition();
    		text = "School: " + spnnrTeam.getItemAtPosition(pos).toString();

    		Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    		toast.show();

    		initTeam();
    	}
    }
    
    
    
}